<?php

namespace app\api\controller;
//导入基类
use app\common\controller\Api;
use app\common\model\User;
use think\Db;
use fast\Random;
/**
 * 登录接口
 */
class Login extends Api
{

    /**
     * 登录
     * @param int $code code值
     * @param char $nickname 昵称
     * @param char $head_image 头像
     * @param char $mobile 手机号
     *  @ApiReturn   ({
    "code": "200",
    "data": {
    "token": 111,
    "type": 1,1第一次登录，调起获取手机号权限，2不需要调
    }
    "msg": "登录成功",
    })
     **/
    public function login(){
        $code = input('code');
        $nickname = input('nickname');
        $head_image = input('head_image');
        //$mobile = input('mobile');
        $appid = 'wx7496026fb84d3edf';
        $appsecret = '2f8b284476159fd09bc501a16f627dc6';
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $appsecret. "&js_code=" . $code . "&grant_type=authorization_code";
        $arr = $this->vget($url);  // 一个使用curl实现的get方法请求
        $arr = json_decode($arr, true);
        $openid = $arr['openid'];
        if(isset($openid)) {
            $list = User::where('openid', $openid)->find();
            $user_token = Random::uuid();
            if ($list) {
                //如果用户已存在，直接登陆
                $result = User::where('openid', $openid)->update(['token' => $user_token]);
                $type =2;
            } else {
                $user_info = [
                    'head_image' => $head_image,
                    'username' => $this->filterEmoji($nickname),
                    'openid' => $openid,
                    //'mobile' => $mobile,
                    'member_card' => 'HG'.randStr(10),
                    'token' => $user_token,
                ];
                $result = User::create($user_info);//user表
                $type =1;
            }
            if($result){
                $data = [
                    'token'=>$user_token,
                    'type'=>$type
                ];
                $this->success($data,'登录成功');
            }else{
                $this->error('登录失败');
            }
        }else{
            $this->error('Access_token获取失败');
        }
    }
    public function vget($url){
        $info=curl_init();
        curl_setopt($info,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($info,CURLOPT_HEADER,0);
        curl_setopt($info,CURLOPT_NOBODY,0);
        curl_setopt($info,CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($info,CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($info,CURLOPT_URL,$url);
        $output= curl_exec($info);
        curl_close($info);
        return $output;
    }
    // 过滤掉emoji表情
    public function filterEmoji($nickname)
    {
        $strEncode = '';
        $length = mb_strlen($nickname,'utf-8');
        for ($i=0; $i < $length; $i++) {
            $_tmpStr = mb_substr($nickname,$i,1,'utf-8');
            if(strlen($_tmpStr) >= 4){
                $strEncode .='.';
            }else{
                $strEncode .= $_tmpStr;
            }
        }
        return $strEncode;
    }
}